package com.kkk.shop.head.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.kkk.shop.common.controller.BaseController;
import com.kkk.shop.common.util.ConstantFinalUtil;
import com.kkk.shop.common.util.PageInfoUtil;
import com.kkk.shop.user.pojo.AUsers;
import com.kkk.shop.user.pojo.AUsersAddress;
import com.kkk.shop.user.service.IUserService;

/**
 * 用户前台登录后的controller
 * @author kkk
 * */
@Controller
@RequestMapping("/head/users/")
public class UsersHeadController extends BaseController
{
	@Resource
	private IUserService userService;
	
	/**
	 * 登录后的首页
	 * @return String
	 * */
	@RequestMapping("/main")
	public String main()
	{
		ConstantFinalUtil.loggerMsg.info("---main-----");
		return "/head/main";
	}
	/**
	 * 地址列表
	 * @param request
	 * */
	@RequestMapping("/addressList")
	public String addressList(HttpServletRequest request)
	{
		ConstantFinalUtil.loggerMsg.info("----addressList-----");
		HttpSession session=request.getSession();
		AUsers users=(AUsers)session.getAttribute("users");
		//查询当前用户的地址信息
		PageInfoUtil pageInfoUtil=this.processPageInfo(request);
		Map<String,Object> paramMap=this.getParamMap();
		paramMap.put("usersId",users.getId());
		//用户可能具有多个地址
		List<AUsersAddress> usersAddressList=this.userService.queryMultiAUsersAddressService(pageInfoUtil, paramMap);
		
		request.setAttribute("usersAddressList", usersAddressList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		return "/head/users/addressList";
	}
	/**
	 * 地址添加
	 * @param request
	 * @return String
	 * */
	@RequestMapping("/addressInsert")
	public String addressInsert(HttpServletRequest request)
	{
		ConstantFinalUtil.loggerMsg.info("----addressInsert----");
		return "/head/users/addressInsert" ;
	}
	
	/**
	 * 地址添加提交
	 * @return String
	 * */
	@RequestMapping("/addressInsertSubmit")
	public String addressInsertSubmit(HttpServletRequest request,AUsersAddress usersAddress)
	{
		ConstantFinalUtil.loggerMsg.info("-----addressInsertSubmit-----");
		HttpSession session=request.getSession();
		AUsers users=(AUsers)session.getAttribute("users");
		usersAddress.setUsersId(users.getId());
		usersAddress.setCreateTime(new Date());
		usersAddress.setUpdateTime(new Date());
		usersAddress.setPubTime(new Date());
		
		JSONObject resultJSON=this.userService.insertOneAUsersAddressService(usersAddress);
		this.info=resultJSON.getString("info");
		request.setAttribute("info",this.info);
		return this.addressInsert(request);
	}
	/**
	 * 地址更新
	 * @param request
	 * @return String
	 * */
	@RequestMapping("/addressUpdate")
	public String addressUpdate(HttpServletRequest request)
	{
		ConstantFinalUtil.loggerMsg.info("-------addressUpdate-------");
		//按照id查询用户地址
		String uaid=request.getParameter("uaid");
		Map<String,Object> paramMap=this.getParamMap();
		paramMap.put("id", uaid);
		AUsersAddress usersAddress=this.userService.queryOneAUsersAddressService(paramMap);
		request.setAttribute("usersAddress", usersAddress);
		return "/head/users/addressUpdate";
	}
	
	/**
	 * 地址更新提交
	 * @param request
	 * @return String
	 * */
	@RequestMapping("/addressUpdateSubmit")
	public String addressUpdateSubmit(HttpServletRequest request)
	{
		ConstantFinalUtil.loggerMsg.info("------addressUpdateSubmit------");
		//按照id查询用户地址
		String uaid=request.getParameter("uaid");
		Map<String,Object> paramMap=this.getParamMap();
		paramMap.put("id", uaid);
		AUsersAddress usersAddress=this.userService.queryOneAUsersAddressService(paramMap);
		request.setAttribute("usersAddress", usersAddress);
		
		String regionId = request.getParameter("regionId");
		String name = request.getParameter("name");
		String contact = request.getParameter("contact");
		String phone = request.getParameter("phone");
		String address = request.getParameter("address");
		String zipCode = request.getParameter("zipCode");
		String isDef = request.getParameter("isDef");
		String status = request.getParameter("status");
		
		usersAddress.setRegionId(Integer.valueOf(regionId));
		usersAddress.setName(name);
		usersAddress.setContact(contact);
		usersAddress.setPhone(phone);
		usersAddress.setAddress(address);
		usersAddress.setZipCode(zipCode);
		usersAddress.setIsDef(Byte.valueOf(isDef));
		usersAddress.setStatus(Byte.valueOf(status));
		usersAddress.setAddress(address);
		usersAddress.setUpdateTime(new Date());
		
		JSONObject resultJSON=this.userService.updateOneAUsersAddressService(usersAddress);
		this.info=resultJSON.getString("info");
		request.setAttribute("info",this.info);
		return this.addressUpdate(request);
	}
	
	/**
	 * 退出
	 * @param request
	 * @param response
	 * @return String
	 * @throws IOException
	 */
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		ConstantFinalUtil.loggerMsg.info("-------logout--------");
		HttpSession session=request.getSession();
		AUsers users=(AUsers)session.getAttribute("users");
		/* 还得操作用户中心 */
		/* 如何客户端跳转到用户中心退出的URL
		 * 通知在用户中心操作token(删除该token)
		 *  */
		/* 跳转到用户中心的登陆页面,并且将returnUrl带上去 */
		String userCenterLoginUrl=ConstantFinalUtil.BUNDLE.getString("users.center.head.logoutUrl")+"&token="+users.getToken();
		ConstantFinalUtil.loggerMsg.info("-----userCenterLoginUrl--{}----",userCenterLoginUrl);
		//进行客户端跳转
		response.sendRedirect(userCenterLoginUrl);
		return null;
	}
}
